<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/index/user/useradd' }">新增用户</el-breadcrumb-item>
    </el-breadcrumb>

    <el-row class="margintop20">
      <el-col :span="12">
        <el-form
          :model="form"
          status-icon
          :rules="rules"
          ref="myform"
          label-width="100px"
          class="myform"
        >
          <el-form-item label="手机号" prop="mobile">
            <el-input type="text" v-model="form.mobile"></el-input>
          </el-form-item>
          <el-form-item label="用户名" prop="username">
            <el-input type="text" v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="角色" prop="roleid">
            <el-select v-model="form.roleid">
              <el-option
                v-for="(role,i) in rolelist"
                :key="i"
                :label="role.label"
                :value="role.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" show-password v-model="form.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="success" class="width100" @click="addUser">新增</el-button>
            <el-button type="primaryr" class="width100" @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>





<script>
import {rolelist} from "@/utils/index"
import {adduser} from '@/api'
export default {
  data() {
    return {
      form: {
        password:1234,
        username:'莫名'
      },
      rolelist,
      rules: {
        //表单校验
        mobile: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              var mobileReg = /^1(3|5|6|7|8|9)\d{9}$/;
              if (mobileReg.test(value)) {
                callback();
              } else {
                callback(new Error("请输入正确的手机号"));
              }
            },
            trigger: "change",
          },
        ],
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              var passReg = /^[A-Za-z0-9]{4,12}$/;
              if (value.length < 4) {
                callback(new Error("密码长度至少4位"));
              } else if (value.length > 12) {
                callback(new Error("密码长度最多12位"));
              } else if (!passReg.test(value)) {
                callback(new Error("密码格式不正确"));
              } else {
                callback();
              }
            },
            trigger: "change",
          },
        ],
        roleid: [
          {required: true, massage: "请选择用户权限等级", trigger: "change" },
        ],
        usrname: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    resetForm() {
      this.$refs.myform.resetFields();
    },
    addUser(){
        this.$refs.myform.validate(valid=>{
          if(valid){
            adduser(this.form)
            .then(res=>{
              
            })
            .catch(err=>{

            })
          }else{
            this.$message.error('废物，添加都不会')
            return false;
          }
        })
    }
  },
};
</script>





<style lang="scss" scoped>
.width100{
  width: 47%;
}
</style>